# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms.dm.route.dm_route_area_effect_base_dt import jms_dm__dm_route_area_effect_base_dt

label = f"dm_route_area_effect_base_dt_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dm__dm_route_area_effect_base_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_route_area_effect_base_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_route_area_effect_base_dt PARTITION (
         p{{{{ execution_date | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
          "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_route_area_effect_base_dt/dt={{{{ execution_date | cst_ds }}}}/*"
        )
        INTO TABLE dm_route_area_effect_base_dt
        FORMAT AS 'PARQUET'(
            in_from_code
           ,in_from_name
           ,in_collect_code
           ,in_collect_name
           ,in_to_code
           ,in_to_name
           ,in_from_area_code
           ,in_from_area_desc
           ,in_from_city_code
           ,in_from_city_desc
           ,in_from_provider_code
           ,in_from_provider_desc
           ,in_from_financial_center_code
           ,in_from_financial_center_desc
           ,in_from_regional_code
           ,in_from_regional_desc
           ,out_to_code
           ,out_to_name
           ,out_collect_code
           ,out_collect_name
           ,out_from_code
           ,out_from_name
           ,out_to_area_code
           ,out_to_area_desc
           ,out_to_city_code
           ,out_to_city_desc
           ,out_to_provider_code
           ,out_to_provider_desc
           ,out_to_financial_center_code
           ,out_to_financial_center_desc
           ,out_to_regional_code
           ,out_to_regional_desc
           ,route_flow
           ,is_main_route
           ,whole_route
           ,is_main_city
           ,total_days_use
           ,total_time_use
           ,total_days_t
           ,latest_warehousing_time_quantum
           ,platform_code_cn
           ,platform_cn
           ,effective_times_cn
           ,platform_code_zt
           ,platform_zt
           ,effective_times_zt
           ,platform_code_sf
           ,platform_sf
           ,effective_times_sf
           ,platform_code_yt
           ,platform_yt
           ,effective_times_yt
           ,platform_code_yd
           ,platform_yd
           ,effective_times_yd
           ,platform_code_thd
           ,platform_thd
           ,effective_times_thd
           ,platform_code_zjs
           ,platform_zjs
           ,effective_times_zjs
           ,cn_cp_line_cnt
           ,zt_cp_line_cnt
           ,sf_cp_line_cnt
           ,yt_cp_line_cnt
           ,yd_cp_line_cnt
           ,thd_cp_line_cnt
           ,zjs_cp_line_cnt
           ,cn_my_line_cnt
           ,zt_my_line_cnt
           ,sf_my_line_cnt
           ,yt_my_line_cnt
           ,yd_my_line_cnt
           ,thd_my_line_cnt
           ,zjs_my_line_cnt
           ,all_line_cnt
           ,cn_cp_cnt
           ,zt_cp_cnt
           ,sf_cp_cnt
           ,yt_cp_cnt
           ,yd_cp_cnt
           ,thd_cp_cnt
           ,zjs_cp_cnt
           ,cn_my_cnt
           ,zt_my_cnt
           ,sf_my_cnt
           ,yt_my_cnt
           ,yd_my_cnt
           ,thd_my_cnt
           ,zjs_my_cnt
           ,all_cnt
        )
        columns from path as (dt)
        set(
             in_from_code                      = in_from_code
            ,in_from_name                      = in_from_name
            ,in_collect_code                   = in_collect_code
            ,in_collect_name                   = in_collect_name
            ,in_to_code                        = in_to_code
            ,in_to_name                        = in_to_name
            ,in_from_area_code                 = in_from_area_code
            ,in_from_area_desc                 = in_from_area_desc
            ,in_from_city_code                 = in_from_city_code
            ,in_from_city_desc                 = in_from_city_desc
            ,in_from_provider_code             = in_from_provider_code
            ,in_from_provider_desc             = in_from_provider_desc
            ,in_from_financial_center_code     = in_from_financial_center_code
            ,in_from_financial_center_desc     = in_from_financial_center_desc
            ,in_from_regional_code             = in_from_regional_code
            ,in_from_regional_desc             = in_from_regional_desc
            ,out_to_code                       = out_to_code
            ,out_to_name                       = out_to_name
            ,out_collect_code                  = out_collect_code
            ,out_collect_name                  = out_collect_name
            ,out_from_code                     = out_from_code
            ,out_from_name                     = out_from_name
            ,out_to_area_code                  = out_to_area_code
            ,out_to_area_desc                  = out_to_area_desc
            ,out_to_city_code                  = out_to_city_code
            ,out_to_city_desc                  = out_to_city_desc
            ,out_to_provider_code              = out_to_provider_code
            ,out_to_provider_desc              = out_to_provider_desc
            ,out_to_financial_center_code      = out_to_financial_center_code
            ,out_to_financial_center_desc      = out_to_financial_center_desc
            ,out_to_regional_code              = out_to_regional_code
            ,out_to_regional_desc              = out_to_regional_desc
            ,route_flow                        = route_flow
            ,is_main_route                     = is_main_route
            ,whole_route                       = whole_route
            ,is_main_city                      = is_main_city
            ,total_days_use                    = total_days_use
            ,total_time_use                    = total_time_use
            ,total_days_t                      = total_days_t
            ,latest_warehousing_time_quantum   = latest_warehousing_time_quantum
            ,platform_code_cn                  = platform_code_cn
            ,platform_cn                       = platform_cn
            ,effective_times_cn                = effective_times_cn
            ,platform_code_zt                  = platform_code_zt
            ,platform_zt                       = platform_zt
            ,effective_times_zt                = effective_times_zt
            ,platform_code_sf                  = platform_code_sf
            ,platform_sf                       = platform_sf
            ,effective_times_sf                = effective_times_sf
            ,platform_code_yt                  = platform_code_yt
            ,platform_yt                       = platform_yt
            ,effective_times_yt                = effective_times_yt
            ,platform_code_yd                  = platform_code_yd
            ,platform_yd                       = platform_yd
            ,effective_times_yd                = effective_times_yd
            ,platform_code_thd                 = platform_code_thd
            ,platform_thd                      = platform_thd
            ,effective_times_thd               = effective_times_thd
            ,platform_code_zjs                 = platform_code_zjs
            ,platform_zjs                      = platform_zjs
            ,effective_times_zjs               = effective_times_zjs
            ,cn_cp_line_cnt                    = cn_cp_line_cnt
            ,zt_cp_line_cnt                    = zt_cp_line_cnt
            ,sf_cp_line_cnt                    = sf_cp_line_cnt
            ,yt_cp_line_cnt                    = yt_cp_line_cnt
            ,yd_cp_line_cnt                    = yd_cp_line_cnt
            ,thd_cp_line_cnt                   = thd_cp_line_cnt
            ,zjs_cp_line_cnt                   = zjs_cp_line_cnt
            ,cn_my_line_cnt                    = cn_my_line_cnt
            ,zt_my_line_cnt                    = zt_my_line_cnt
            ,sf_my_line_cnt                    = sf_my_line_cnt
            ,yt_my_line_cnt                    = yt_my_line_cnt
            ,yd_my_line_cnt                    = yd_my_line_cnt
            ,thd_my_line_cnt                   = thd_my_line_cnt
            ,zjs_my_line_cnt                   = zjs_my_line_cnt
            ,all_line_cnt                      = all_line_cnt
            ,cn_cp_cnt                         = cn_cp_cnt
            ,zt_cp_cnt                         = zt_cp_cnt
            ,sf_cp_cnt                         = sf_cp_cnt
            ,yt_cp_cnt                         = yt_cp_cnt
            ,yd_cp_cnt                         = yd_cp_cnt
            ,thd_cp_cnt                        = thd_cp_cnt
            ,zjs_cp_cnt                        = zjs_cp_cnt
            ,cn_my_cnt                         = cn_my_cnt
            ,zt_my_cnt                         = zt_my_cnt
            ,sf_my_cnt                         = sf_my_cnt
            ,yt_my_cnt                         = yt_my_cnt
            ,yd_my_cnt                         = yd_my_cnt
            ,thd_my_cnt                        = thd_my_cnt
            ,zjs_my_cnt                        = zjs_my_cnt
            ,all_cnt                           = all_cnt
            ,dt                                = dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dm__dm_route_area_effect_base_dt << jms_dm__dm_route_area_effect_base_dt
